<?php 
require_once(PHPLIB_ROOT . 'lib' . DIRECTORY_SEPARATOR . 'Template.php');

class ITemplate extends Template{
	
	public function setContentTPL($file){
		$this->set_file("content_handler", $file);
	}
	
	private function setCss($css){
		if( !$css ){
			$this->set_var("i_css", "" );
			return;
		}
		
		$css = is_array($css) ? $css :  array( $css );
		
		foreach($css as &$item){
			$item = '<link rel="stylesheet" type="text/css" href="/static/css/' . $item . '"/>';
		}
		
		$this->set_var("i_css", implode("\n", $css) );
	}
	
	private function setJs($js){
		if( !$js ){
			$this->set_var("i_js", "" );
			return;
		}
		
		$js = is_array($js) ? $js : array( $js );

		foreach($js as &$item){
			$src =  substr($item, 0, 1) === "/" ? $item : ( '/static/js/' . $item );
			$item = '<script type="text/javascript" src="' . $src . '"></script>';
		}
		
		$this->set_var("i_js", implode("\n", $js) );
	}
	
	public function out($opt = array()){
		$this->set_file("base_handler", "base.tpl");
		$this->setCss( isset($opt['css']) ? $opt['css'] : null );
		$this->setJs(  isset($opt['js']) ? $opt['js'] : null  );
		$this->set_var("i_before_body", isset($opt["before_body"]) ? $opt["before_body"] : "" );
		$this->set_var("i_after_body", isset($opt["after_body"]) ? $opt["after_body"] : "" );
		$this->set_var("i_title", isset($opt["title"]) ? $opt["title"] : "" );
		$this->parse("i_content", "content_handler");
		return $this->parse("output", "base_handler");
	}
	
	public function setMessage($opt){
		
		$opt = is_array( $opt ) ? $opt : array('message' => $opt);
		$this->set_file(array(
			"base_handler" =>"base.tpl",
			"content_handler" => "message.tpl"
		));
		$this->setCss( isset($opt['css']) ? $opt['css'] : null );
		$this->setJs(  isset($opt['js']) ? $opt['js'] : null  );
		$this->set_var("i_before_body", isset($opt["before_body"]) ? $opt["before_body"] : "" );
		$this->set_var("i_after_body", isset($opt["after_body"]) ? $opt["after_body"] : "" );
		$this->set_var("i_title", isset($opt["title"]) ? $opt["title"] : "" );
		$this->set_var("i_message", isset($opt["message"]) ? $opt["message"] : '' );
		$MESSAGE_TYPE = array("warn" => "warn", "error" => "errno", "succees" => "success");
		$this->set_var("i_message_type", isset($opt["type"]) && isset($MESSAGE_TYPE[$opt['type']]) ? $MESSAGE_TYPE[$opt['type']] : 'warn' );
		$this->parse("i_content", "content_handler");
		return $this->parse("output", "base_handler");
	}
	
}